package com.example.arithmeticleetcode.arithmetic;

/**
 * @program: arithmetic-leetcode
 * @description: 插入排序 稳定排序 时间复杂度O(n^2)
 * @author: FangZhen
 * @create: 2020-04-15 11:25
 **/
public class InsertSort {

    public static void main(String[] args) {
        int[] arr = {1, 5, 2, 4};
        insertSortTest01(arr);
    }

    public static void insertSortTest01(int[] arr) {
        int temp;
        for (int i = 1, len = arr.length; i < len; i++) {
            temp = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > temp) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = temp;
        }
        for (int i : arr) {
            System.out.println(i);
        }
    }
}
